package com.gzy.rpsm_property.mapper;

import com.gzy.rpsm_property.pojo.Attention;
import com.gzy.rpsm_property.pojo.User;
import com.gzy.rpsm_property.pojo.Users;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;

public interface AttentionMapper {
//    @Insert("INSERT INTO `attention` (`from_id`, `to_id`, `attend_date`) " +
//            "VALUES ((SELECT `userid` FROM `user` WHERE `username` = #{fromUsername}), " +
//            "(SELECT `userid` FROM `user` WHERE `username` = #{toUsername}), " +
//            "CURDATE())")
//    void insertAttention(@Param("fromUsername") String fromUsername, @Param("toUsername") String toUsername);

    @Select("SELECT a.attention_id, u1.username AS from_username, u2.username AS to_username, a.attend_date " +
            "FROM attention a " +
            "JOIN user u1 ON a.from_id = u1.userid " +
            "JOIN user u2 ON a.to_id = u2.userid")
    List<Map<String, Object>> getAttentionList();
    @Select("SELECT u.* " +
            "FROM user u " +
            "JOIN attention a ON u.userid = a.to_id " +
            "WHERE a.from_id = (SELECT userid FROM user WHERE username = #{username})")
    List<Users> getFollowingUsersByUsername(@Param("username") String username);

    @Select("SELECT u.* " +
            "FROM user u " +
            "JOIN attention a ON u.userid = a.from_id " +
            "WHERE a.to_id = (SELECT userid FROM user WHERE username = #{username})")
    List<Users> getFollowersByUsername(@Param("username") String username);

    @Insert("INSERT INTO attention (from_id, to_id, attend_date) VALUES (#{fromId}, #{toId}, #{attendDate})")
    @Options(useGeneratedKeys = true, keyProperty = "attentionId")
    void insertAttention(Attention attention);

    @Select("SELECT COUNT(*) FROM attention WHERE from_id = #{fromId} AND to_id = #{toId}")
    int countExistingAttention(@Param("fromId") int fromId, @Param("toId") int toId);

}
